package cc.rengu.igas.channel.upqc.core.service.trans;


import cc.rengu.igas.channel.upqc.common.constant.UpqcParamConstant;
import cc.rengu.igas.channel.upqc.common.constant.UpqcTreeNodeConstant;
import cc.rengu.igas.channel.upqc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upqc.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upqc.core.service.base.PayService;
import cc.rengu.igas.channel.upqc.facade.bean.AddnCondition;
import cc.rengu.igas.channel.upqc.facade.bean.PayerInfo;
import cc.rengu.igas.channel.upqc.facade.request.C2BAddDealNoticeRequest;
import cc.rengu.igas.channel.upqc.facade.response.C2BAddDealNoticeResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import org.apache.commons.codec.binary.Base64;

/**
 * C2B附加处理通知
 * 被扫银联二维码验密通知支付(801303)
 */
public class C2bAddDealNoticeService extends PayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        C2BAddDealNoticeRequest c2bAddDealNoticeRequest = new C2BAddDealNoticeRequest();
        ConvertUtil.convertOutput(c2bAddDealNoticeRequest);
        return c2bAddDealNoticeRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpqcTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        C2BAddDealNoticeResponse c2bAddDealNoticeResponse = new C2BAddDealNoticeResponse();
        C2BAddDealNoticeRequest c2bAddDealNoticeRequest = (C2BAddDealNoticeRequest) request;
        ConvertUtil.convertInput(c2bAddDealNoticeRequest);
        packagePayerInfo(c2bAddDealNoticeRequest.getPayerInfo());
        convertRiskInfo(c2bAddDealNoticeRequest.getRiskInfoBean());
        packageAddnCond(c2bAddDealNoticeRequest.getAddnCondition());
        /* 根据通道标识调用不同通道进行支付 */
        xmlTreeUtil.setXmlTreeStringValue(UpqcTreeNodeConstant.txnType, c2bAddDealNoticeRequest.getHeader().getTxnNum());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, c2bAddDealNoticeRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        /* 恢复原交易码 */
        if (98 == iReturnCode) {
            rglog.error("调用银联二维码服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.TIME_OUT_ERROR.getRespCode(), RespCodeEnum.TIME_OUT_ERROR.getRespDesc());
        } else if (0 != iReturnCode) {
            rglog.error("调用银联二维码服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(c2bAddDealNoticeResponse);
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        if (UpqcParamConstant.SUCCESS_CODE.equals(c2bAddDealNoticeResponse.getRespCode())) {
            c2bAddDealNoticeResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            c2bAddDealNoticeResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        }
        chnlBizResponse.setResult(c2bAddDealNoticeResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        C2BAddDealNoticeResponse c2bAddDealNoticeResponse = (C2BAddDealNoticeResponse) response.getResult();
        ConvertUtil.convertInput(c2bAddDealNoticeResponse);
    }

    private void packagePayerInfo(PayerInfo payerInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        if (payerInfo != null && isNotEmpty(payerInfo)) {
            StringBuffer payerInfoBuff = new StringBuffer(UpqcParamConstant.LEFT_BRACE);
            if (null != payerInfo.getAccNo() && !payerInfo.getAccNo().isEmpty()) {
                payerInfoBuff.append("accNo" + UpqcParamConstant.EQUAL + payerInfo.getAccNo() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getName() && !payerInfo.getName().isEmpty()) {
                payerInfoBuff.append("name" + UpqcParamConstant.EQUAL + payerInfo.getName() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getPayerBankInfo() && !payerInfo.getPayerBankInfo().isEmpty()) {
                payerInfoBuff.append("payerBankInfo" + UpqcParamConstant.EQUAL + payerInfo.getPayerBankInfo() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getIssCode() && !payerInfo.getIssCode().isEmpty()) {
                payerInfoBuff.append("issCode" + UpqcParamConstant.EQUAL + payerInfo.getIssCode() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getCertifTp() && !payerInfo.getCertifTp().isEmpty()) {
                payerInfoBuff.append("certifTp" + UpqcParamConstant.EQUAL + payerInfo.getCertifTp() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getCertifId() && !payerInfo.getCertifId().isEmpty()) {
                payerInfoBuff.append("certifId" + UpqcParamConstant.EQUAL + payerInfo.getCertifId() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getCvn2() && !payerInfo.getCvn2().isEmpty()) {
                payerInfoBuff.append("cvn2" + UpqcParamConstant.EQUAL + payerInfo.getCvn2() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getExpired() && !payerInfo.getExpired().isEmpty()) {
                payerInfoBuff.append("expired" + UpqcParamConstant.EQUAL + payerInfo.getExpired() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getMobile() && !payerInfo.getMobile().isEmpty()) {
                payerInfoBuff.append("mobile" + UpqcParamConstant.EQUAL + payerInfo.getMobile() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getCardAttr() && !payerInfo.getCardAttr().isEmpty()) {
                payerInfoBuff.append("cardAttr" + UpqcParamConstant.EQUAL + payerInfo.getCardAttr() + UpqcParamConstant.AMPERSAND);
            }
            payerInfoBuff.setLength(payerInfoBuff.length() - 1);
            payerInfoBuff.append(UpqcParamConstant.RIGHT_BRACE);
            xmlTreeUtil.setXmlTreeStringValue(UpqcTreeNodeConstant.payInfoStr, encData(payerInfoBuff.toString()));
        }
    }

    private void packageAddnCond(AddnCondition addnCondition) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        if (addnCondition != null && isNotEmpty(addnCondition)) {
            StringBuffer addnConditionBuff = new StringBuffer(UpqcParamConstant.LEFT_BRACE);
            if (null != addnCondition.getCurrency() && !addnCondition.getCurrency().isEmpty()) {
                addnConditionBuff.append("currency" + UpqcParamConstant.EQUAL + addnCondition.getCurrency() + UpqcParamConstant.AMPERSAND);
            }
            if (null != addnCondition.getPinFree() && !addnCondition.getPinFree().isEmpty()) {
                addnConditionBuff.append("pinFree" + UpqcParamConstant.EQUAL + AmountUtil.changeY2F(addnCondition.getPinFree()) + UpqcParamConstant.AMPERSAND);
            }
            if (null != addnCondition.getMaxAmt() && !addnCondition.getMaxAmt().isEmpty()) {
                addnConditionBuff.append("maxAmont" + UpqcParamConstant.EQUAL + AmountUtil.changeY2F(addnCondition.getMaxAmt()) + UpqcParamConstant.AMPERSAND);
            }
            addnConditionBuff.setLength(addnConditionBuff.length() - 1);
            addnConditionBuff.append(UpqcParamConstant.RIGHT_BRACE);
            xmlTreeUtil.setXmlTreeStringValue(UpqcTreeNodeConstant.addnCondStr, Base64.encodeBase64String(addnConditionBuff.toString().getBytes()));
        }
    }
}
